package edu.nus.iss.t5.jass.view;

import javax.swing.table.AbstractTableModel;
import edu.nus.iss.t5.jass.entity.*;

import java.util.Date;
import java.util.List;


/**
 * This class create Result Table by extending AbstractTableModel.
 * @author TMH, Team
 */
class ResultJobAdvertisementTable extends AbstractTableModel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private boolean DEBUG = false;
	String[] tableTitles = {"S/N","Job Title","Description",
			"Start Date","Creation Date", "Expired Date", "Owner", "Status", "Company Description"};
	//example data . it will change by Object passed by others
/*	Object[][] data = {
		    {new Boolean(false),  new Integer(5), "Application Specialist","J2EE Struts", 
		    	"30/10/2010", "30/10/2010", "30/10/2010", "Tin Maung Htay", "Pending", "CSC"}, 
		    {new Boolean(false),  new Integer(4), "Application Specialist 2 ","J2EE Struts 2",  
			    "30/10/2010", "30/10/2010", "30/10/2010", "Tin Maung Htay", "Pending", "CSC"}
		};
		*/
	Object[][] data = new Object [0][0];
    public ResultJobAdvertisementTable (){};
    public void updateData (List<JobAdvertisement> jl){
    	if (jl.size()>0){
    		data = new Object [jl.size()][9];
    		for (int i=0;i<jl.size();i++){
    				data[i][0]= new Integer(i+1);
    				data[i][1]= jl.get(i).title;
    				data[i][2]= jl.get(i).jobDescription;
    				if (jl.get(i).getCreationDate()!=null) {data[i][3]= jl.get(i).getCreationDate();}
    				else {data[i][3]= new Date();}
    				if (jl.get(i).getCreationDate()!=null) {data[i][4]= jl.get(i).getCreationDate();}
    				else {data[i][4]= new Date();}    	
    				if (jl.get(i).getExpiryDate()!=null) {data[i][5]= jl.get(i).getExpiryDate();}
    				else {data[i][5]= new Date();}  
    				if (jl.get(i).getOwner()!=null) {data[i][6]= jl.get(i).getOwner();}
    				else {data[i][6]= "";}
    				data[i][7]= jl.get(i).toStringStatus();
    				if (jl.get(i).getCompany()!=null) {data[i][8]= jl.get(i).getCompany();}
    				else {data[i][8]= "";}
    				//System.out.println("in result: "+jl.get(i).baseCode);    				
    		}
    	}
    	else data = new Object [0][0];
    };

    public int getColumnCount() {
        return tableTitles.length;
    }

    public int getRowCount() {
        return data.length;
    }

    public String getColumnName(int col) {
        return tableTitles[col];
    }

    public Object getValueAt(int row, int col) {
        return data[row][col];
    }

    /*
     * JTable uses this method to determine the default renderer/
     * editor for each cell.  If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
     */
    public Class getColumnClass(int c) {
 	
        return getValueAt(0, c).getClass();
    }

    /*
     * Don't need to implement this method unless your table's
     * editable.
     */
    public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
        if (col < 2) {
            return false;
        } else {
            return true;
        }
    }

    /*
     * Don't need to implement this method unless your table's
     * data can change.
     */
    public void setValueAt(Object value, int row, int col) {
        if (DEBUG) {
            System.out.println("Setting value at " + row + "," + col
                               + " to " + value
                               + " (an instance of "
                               + value.getClass() + ")");
        }

        data[row][col] = value;
        fireTableCellUpdated(row, col);

        if (DEBUG) {
            System.out.println("New value of data:");
            printDebugData();
        }
    }

    private void printDebugData() {
        int numRows = getRowCount();
        int numCols = getColumnCount();

        for (int i=0; i < numRows; i++) {
            System.out.print("    row " + i + ":");
            for (int j=0; j < numCols; j++) {
                System.out.print("  " + data[i][j]);
            }
            System.out.println();
        }
        System.out.println("--------------------------");
    }
}